/*     */ import java.awt.BorderLayout;
/*     */ import java.awt.Color;
/*     */ import java.awt.Font;
/*     */ import java.awt.event.WindowAdapter;
/*     */ import java.awt.event.WindowEvent;
/*     */ import java.io.BufferedInputStream;
/*     */ import java.io.BufferedOutputStream;
/*     */ import java.io.BufferedReader;
/*     */ import java.io.File;
/*     */ import java.io.FileNotFoundException;
/*     */ import java.io.FileOutputStream;
/*     */ import java.io.InputStream;
/*     */ import java.io.InputStreamReader;
/*     */ import java.io.OutputStream;
/*     */ import java.io.PrintStream;
/*     */ import java.net.ConnectException;
/*     */ import java.net.MalformedURLException;
/*     */ import java.net.URL;
/*     */ import java.net.URLConnection;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Arrays;
/*     */ import java.util.List;
/*     */ import java.util.zip.ZipEntry;
/*     */ import java.util.zip.ZipInputStream;
/*     */ import javax.swing.BoxLayout;
/*     */ import javax.swing.JFrame;
/*     */ import javax.swing.JLabel;
/*     */ import javax.swing.JOptionPane;
/*     */ import javax.swing.JPanel;
/*     */ import javax.swing.JProgressBar;
/*     */ import javax.swing.plaf.basic.BasicProgressBarUI;
/*     */ 
/*     */ public class FileDownloader
/*     */ {
/*     */   static final int BUFFER = 2048;
/*  20 */   public List<String> datesToRead = new ArrayList();
/*  21 */   private List<String> datesInFile = new ArrayList();
/*  22 */   private List<String> datesToDelete = new ArrayList();
/*     */   private JLabel dloadStatus;
/*     */   private JFrame dloadFrame;
/*     */   private JPanel dloadPanel;
/*     */   private JProgressBar pbar;
/*     */ 
/*     */   private void buildDloadFrame()
/*     */   {
/*  39 */     this.dloadFrame = new JFrame("Market Analysis - Deebak");
/*  40 */     this.dloadFrame.addWindowListener(new WindowAdapter()
/*     */     {
/*     */       public void windowClosing(WindowEvent paramWindowEvent)
/*     */       {
/*  44 */         System.out.println("Application launch terminated.");
/*  45 */         System.exit(0);
/*     */       }
/*     */     });
/*  48 */     this.dloadFrame.setResizable(false);
/*     */ 
/*  50 */     this.dloadPanel = new JPanel();
/*  51 */     this.dloadPanel.setLayout(new BorderLayout());
/*     */ 
/*  53 */     this.dloadStatus = new JLabel(" Downloading NSE Files. Please wait...");
/*     */ 
/*  55 */     this.pbar = new JProgressBar();
/*  56 */     this.pbar.setMinimum(1);
/*  57 */     this.pbar.setMaximum(310);
/*  58 */     this.pbar.setBackground(Color.WHITE);
/*  59 */     BasicProgressBarUI local2 = new BasicProgressBarUI() {
/*     */       protected Color getSelectionBackground() {
/*  61 */         return Color.black;
/*     */       }
/*     */       protected Color getSelectionForeground() {
/*  64 */         return Color.black;
/*     */       }
/*     */     };
/*  67 */     this.pbar.setUI(local2);
/*  68 */     this.pbar.setFont(new Font("Sanserif", 0, 12));
/*  69 */     this.pbar.setStringPainted(true);
/*     */ 
/*  71 */     JPanel localJPanel = new JPanel();
/*  72 */     localJPanel.setLayout(new BoxLayout(localJPanel, 3));
/*     */ 
/*  74 */     this.dloadFrame.add(this.dloadPanel);
/*  75 */     localJPanel.add(this.dloadStatus);
/*  76 */     localJPanel.add(new JLabel(" "));
/*  77 */     localJPanel.add(new JLabel(" Note:Launching for NSE first time in your system"));
/*  78 */     localJPanel.add(new JLabel(" will take some time to download the files depending"));
/*  79 */     localJPanel.add(new JLabel(" upon your connection speed. "));
/*  80 */     localJPanel.add(new JLabel(" "));
/*     */ 
/*  82 */     this.dloadPanel.add(localJPanel, "North");
/*  83 */     this.dloadPanel.add(this.pbar, "Center");
/*     */ 
/*  85 */     this.dloadFrame.setSize(350, 155);
/*  86 */     this.dloadFrame.setLocationRelativeTo(null);
/*  87 */     this.dloadFrame.setVisible(true);
/*     */   }
/*     */ 
/*     */   private void setupPath()
/*     */   {
/*  94 */     File localFile = new File("C:\\profitindicators");
/*  95 */     if (!localFile.exists()) {
/*  96 */       localFile.mkdir();
/*     */     }
/*  98 */     localFile = new File("C:\\profitindicators\\data");
/*  99 */     if (!localFile.exists()) {
/* 100 */       localFile.mkdir();
/*     */     }
/* 102 */     localFile = new File("C:\\profitindicators\\data\\nse");
/* 103 */     if (!localFile.exists()) {
/* 104 */       localFile.mkdir();
/*     */     }
/* 106 */     localFile = new File("C:\\profitindicators\\data\\userdata");
/* 107 */     if (!localFile.exists())
/* 108 */       localFile.mkdir();
/*     */   }
/*     */ 
/*     */   public void downloadFiles()
/*     */   {
/* 115 */     buildDloadFrame();
/* 116 */     readDates();
/* 117 */     this.pbar.setMaximum(this.datesInFile.size());
/* 118 */     readDatesToDelete();
/* 119 */     setupPath();
/*     */ 
/* 123 */     System.out.println("Downloading files for NSE...");
/*     */     String str;
/*     */     File localFile1;
/* 124 */     for (int i = 0; i < this.datesInFile.size(); i++)
/*     */     {
/* 126 */       str = "C:\\profitindicators\\data\\nse\\" + (String)this.datesInFile.get(i) + ".csv";
/*     */ 
/* 128 */       localFile1 = new File(str);
/*     */ 
/* 130 */       if (!localFile1.exists()) {
/*     */         try
/*     */         {
/* 133 */           str = "C:\\profitindicators\\data\\nse\\" + (String)this.datesInFile.get(i) + ".csv.zip";
/* 134 */           fileDownloader(i, str);
/* 135 */           unZip(i);
/*     */ 
/* 137 */           File localFile2 = new File(str);
/*     */ 
/* 139 */           if (localFile2.exists()) {
/*     */             try
/*     */             {
/* 142 */               localFile2.delete();
/*     */             }
/*     */             catch (Exception localException4)
/*     */             {
/*     */             }
/*     */           }
/*     */ 
/*     */         }
/*     */         catch (Exception localException1)
/*     */         {
/* 152 */           break;
/*     */         }
/*     */       }
/* 155 */       else if (localFile1.length() < 50000L) {
/*     */         try
/*     */         {
/* 158 */           str = "C:\\profitindicators\\data\\nse\\" + (String)this.datesInFile.get(i) + ".csv.zip";
/* 159 */           fileDownloader(i, str);
/* 160 */           unZip(i);
/*     */ 
/* 162 */           File localFile3 = new File(str);
/*     */ 
/* 164 */           if (localFile3.exists()) {
/*     */             try
/*     */             {
/* 167 */               localFile3.delete();
/*     */             }
/*     */             catch (Exception localException5)
/*     */             {
/*     */             }
/*     */ 
/*     */           }
/*     */ 
/*     */         }
/*     */         catch (Exception localException2)
/*     */         {
/* 179 */           break;
/*     */         }
/*     */       }
/*     */       else
/*     */       {
/* 184 */         this.datesToRead.add(this.datesInFile.get(i));
/*     */       }
/*     */ 
/* 188 */       this.pbar.setValue(i);
/*     */     }
/*     */ 
/* 193 */     System.out.println("Deleting old files...");
/* 194 */     for (int i = 0; i < this.datesToDelete.size(); i++)
/*     */     {
/* 196 */       str = "C:\\profitindicators\\data\\nse\\" + (String)this.datesToDelete.get(i) + ".csv";
/* 197 */       localFile1 = new File(str);
/*     */ 
/* 199 */       if (!localFile1.exists())
/*     */         continue;
/*     */       try {
/* 202 */         localFile1.delete();
/*     */       }
/*     */       catch (Exception localException3) {
/* 205 */         System.out.println("Failed to delete " + (String)this.datesToDelete.get(i) + ".csv");
/*     */       }
/*     */ 
/*     */     }
/*     */ 
/* 212 */     this.dloadFrame.dispose();
/*     */   }
/*     */ 
/*     */   private void fileDownloader(int paramInt, String paramString)
/*     */     throws Exception
/*     */   {
/* 219 */     String str = "http://www.nseindia.com/content/historical/EQUITIES/" + ((String)this.datesInFile.get(paramInt)).substring(7, 11) + "/" + ((String)this.datesInFile.get(paramInt)).substring(3, 6) + "/cm" + ((String)this.datesInFile.get(paramInt)).substring(0, 2) + ((String)this.datesInFile.get(paramInt)).substring(3, 6) + ((String)this.datesInFile.get(paramInt)).substring(7, 11) + "bhav.csv.zip";
/*     */ 
/* 223 */     BufferedOutputStream localBufferedOutputStream = null;
/* 224 */     URLConnection localURLConnection = null;
/* 225 */     InputStream localInputStream = null;
/*     */ 
/* 227 */     ArrayList localArrayList1 = new ArrayList();
/* 228 */     ArrayList localArrayList2 = new ArrayList();
/*     */     try
/*     */     {
/* 232 */       URL localURL = new URL(str);
/*     */ 
/* 234 */       localURLConnection = localURL.openConnection();
/* 235 */       localInputStream = localURLConnection.getInputStream();
/* 236 */       byte[] arrayOfByte = new byte[1024];
int i;
/*     */ 
/* 238 */       while ((i = localInputStream.read(arrayOfByte)) != -1) {
/* 239 */         localArrayList1.add(arrayOfByte.clone());
/* 240 */         localArrayList2.add(Integer.valueOf(i));
/*     */       }
/*     */ 
/* 244 */       localInputStream = localURL.openStream();
/* 245 */       i = localInputStream.read(arrayOfByte);
/*     */ 
/* 247 */       if (!Arrays.equals(arrayOfByte, (byte[])(byte[])localArrayList1.get(0)))
/*     */       {
/* 249 */         throw new ConnectException();
/*     */       }
/*     */ 
/* 253 */       if (localArrayList1.size() < 10)
/*     */       {
/* 255 */         throw new ConnectException();
/*     */       }
/*     */ 
/* 259 */       localBufferedOutputStream = new BufferedOutputStream(new FileOutputStream(paramString));
/*     */ 
/* 261 */       for (int j = 0; j < localArrayList1.size(); j++)
/*     */       {
/* 263 */         localBufferedOutputStream.write((byte[])(byte[])localArrayList1.get(j), 0, ((Integer)localArrayList2.get(j)).intValue());
/*     */       }
/*     */ 
/* 267 */       this.datesToRead.add(this.datesInFile.get(paramInt));
/*     */     }
/*     */     catch (FileNotFoundException localException2)
/*     */     {
/* 275 */       if (paramInt < this.datesInFile.size() - 10)
/*     */       {
/* 281 */         System.out.println("NSE EOD File not found in server for " + (String)this.datesInFile.get(paramInt));
/*     */       }
/*     */ 
/*     */     }
/*     */     catch (ConnectException localConnectException)
/*     */     {
/* 290 */       JOptionPane.showMessageDialog(null, "Connection broken while downloading " + (String)this.datesInFile.get(paramInt), null, 0);
/*     */ 
/* 294 */       throw localConnectException;
/*     */     }
/*     */     catch (Exception localException3)
/*     */     {
/* 299 */       JOptionPane.showMessageDialog(null, "Unable to download EOD file for " + (String)this.datesInFile.get(paramInt) + ". \n" + "Check the folder \"C:\\profitindicators\\data\\nse\" not to be write protected \n" + "Check your internet connection", null, 0);
/*     */ 
/* 305 */       localException3.printStackTrace();
/* 306 */       throw localException3;
/*     */     }
/*     */     finally
/*     */     {
/*     */       try
/*     */       {
/* 314 */         if (localInputStream != null) {
/* 315 */           localInputStream.close();
/*     */         }
/* 317 */         if (localBufferedOutputStream != null) {
/* 318 */           localBufferedOutputStream.close();
/*     */         }
/*     */       }
/*     */       catch (Exception localException4)
/*     */       {
/* 323 */         localException4.printStackTrace();
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   private void unZip(int paramInt)
/*     */   {
/*     */     try
/*     */     {
/* 343 */       URL localURL = new URL("file:///C|/profitindicators/data/nse/" + (String)this.datesInFile.get(paramInt) + ".csv.zip");
/*     */ 
/* 345 */       BufferedOutputStream localBufferedOutputStream = null;
/* 346 */       InputStream localInputStream = localURL.openStream();
/*     */ 
/* 348 */       ZipInputStream localZipInputStream = new ZipInputStream(new BufferedInputStream(localInputStream));
/*     */       ZipEntry localZipEntry;
/* 351 */       while ((localZipEntry = localZipInputStream.getNextEntry()) != null)
/*     */       {
/* 354 */         byte[] arrayOfByte = new byte[2048];
/*     */ 
/* 356 */         File localFile = new File("C:\\profitindicators\\data\\nse\\" + (String)this.datesInFile.get(paramInt) + ".csv");
/* 357 */         FileOutputStream localFileOutputStream = new FileOutputStream(localFile);
/*     */ 
/* 359 */         localBufferedOutputStream = new BufferedOutputStream(localFileOutputStream, 2048);
/*     */         int i;
/* 363 */         while ((i = localZipInputStream.read(arrayOfByte, 0, 2048)) != -1) {
/* 364 */           localBufferedOutputStream.write(arrayOfByte, 0, i);
/*     */         }
/* 366 */         localBufferedOutputStream.flush();
/* 367 */         localBufferedOutputStream.close();
/*     */       }
/* 369 */       localZipInputStream.close();
/*     */     }
/*     */     catch (FileNotFoundException localFileNotFoundException)
/*     */     {
/* 376 */       if (paramInt < this.datesInFile.size() - 10)
/*     */       {
/* 382 */         System.out.println("NSE EOD File not found in server for " + (String)this.datesInFile.get(paramInt));
/*     */       }
/*     */ 
/*     */     }
/*     */     catch (Exception localException)
/*     */     {
/* 394 */       localException.printStackTrace();
/* 395 */       return;
/*     */     }
/*     */   }
/*     */ 
/*     */   private void readDatesToDelete()
/*     */   {
/*     */     try
/*     */     {
/* 410 */       URL localURL = new URL("http://www.profitindicators.com/home/nseDeleteFls.csv");
/*     */ 
/* 412 */       InputStream localInputStream = localURL.openStream();
/* 413 */       BufferedReader localBufferedReader = new BufferedReader(new InputStreamReader(localInputStream));
/*     */ 
/* 415 */       String str = localBufferedReader.readLine();
/* 416 */       str = localBufferedReader.readLine();
/*     */ 
/* 418 */       while (str != null)
/*     */       {
/* 420 */         this.datesToDelete.add(str.trim());
/*     */ 
/* 422 */         str = localBufferedReader.readLine();
/*     */       }
/*     */ 
/* 426 */       localBufferedReader.close();
/*     */     }
/*     */     catch (FileNotFoundException localFileNotFoundException)
/*     */     {
/* 431 */       System.out.println("File deleteFiles.csv not found.");
/*     */     }
/*     */     catch (MalformedURLException localMalformedURLException)
/*     */     {
/* 435 */       System.out.println("URL malformed");
/* 436 */       localMalformedURLException.printStackTrace();
/*     */     }
/*     */     catch (Exception localException)
/*     */     {
/* 440 */       localException.printStackTrace();
/*     */     }
/*     */   }
/*     */ 
/*     */   private void readDates()
/*     */   {
/*     */     try
/*     */     {
/* 452 */       URL localURL = new URL("http://www.profitindicators.com/home/nseDts.csv");
/*     */ 
/* 454 */       InputStream localInputStream = localURL.openStream();
/* 455 */       BufferedReader localBufferedReader = new BufferedReader(new InputStreamReader(localInputStream));
/*     */ 
/* 457 */       String str = localBufferedReader.readLine();
/* 458 */       str = localBufferedReader.readLine();
/*     */ 
/* 460 */       while (str != null)
/*     */       {
/* 462 */         this.datesInFile.add(str.trim());
/*     */ 
/* 464 */         str = localBufferedReader.readLine();
/*     */       }
/*     */ 
/* 468 */       localBufferedReader.close();
/*     */     }
/*     */     catch (FileNotFoundException localFileNotFoundException)
/*     */     {
/* 473 */       System.out.println("File dates.csv not found.");
/*     */     }
/*     */     catch (MalformedURLException localMalformedURLException)
/*     */     {
/* 477 */       System.out.println("URL malformed");
/* 478 */       localMalformedURLException.printStackTrace();
/*     */     }
/*     */     catch (Exception localException)
/*     */     {
/* 482 */       localException.printStackTrace();
/*     */     }
/*     */   }
/*     */ 
/*     */   public static void main(String[] paramArrayOfString)
/*     */   {
/* 491 */     FileDownloader localFileDownloader = new FileDownloader();
/*     */ 
/* 493 */     localFileDownloader.downloadFiles();
/* 494 */     System.out.println("datesInFile");
/* 495 */     System.out.println("" + localFileDownloader.datesInFile.size());
/* 496 */     System.out.println("" + (String)localFileDownloader.datesInFile.get(0));
/* 497 */     System.out.println("" + (String)localFileDownloader.datesInFile.get(localFileDownloader.datesInFile.size() - 1));
/*     */ 
/* 499 */     System.out.println("datesToDelete");
/* 500 */     System.out.println("" + localFileDownloader.datesToDelete.size());
/* 501 */     System.out.println("" + (String)localFileDownloader.datesToDelete.get(0));
/* 502 */     System.out.println("" + (String)localFileDownloader.datesToDelete.get(localFileDownloader.datesToDelete.size() - 1));
/*     */ 
/* 504 */     System.out.println("datesToRead");
/* 505 */     System.out.println("" + localFileDownloader.datesToRead.size());
/* 506 */     System.out.println("" + (String)localFileDownloader.datesToRead.get(0));
/* 507 */     System.out.println("" + (String)localFileDownloader.datesToRead.get(localFileDownloader.datesToRead.size() - 1));
/*     */   }
/*     */ }

/* Location:           C:\Users\Deebak\Downloads\profitindicatorsv2-05.jar
 * Qualified Name:     FileDownloader
 * JD-Core Version:    0.6.0
 */